#include "loginwindow.h"
#include "mainpagewindow.h"
#include "ui_loginwindow.h"
#include <QLabel>
#include <QVBoxLayout>
#include <QLineEdit>
#include <QPushButton>
#include <signal.h>
#include <QtDebug>
#include <QIcon>
#include <QMessageBox>
#include <QDialog>
#include "registerdialog.h"

loginWindow::loginWindow(QWidget *parent)
    : QMainWindow(parent), ui(new Ui::loginWindow),parent(parent)
{
    ui->setupUi(this);

    this->setWindowIcon(QIcon(":/img/bbb.png"));
    this->setWindowTitle("登录");

    /* 自己写代码构造登录页面ui */

    count = 0;

    QWidget *centralWidget = new QWidget(this);
    this->setCentralWidget(centralWidget);

    QPushButton *rebackBtn = new QPushButton("返回",centralWidget);
    rebackBtn->setGeometry(40,40,60,40);
    connect(rebackBtn,SIGNAL(clicked()),this,SLOT(reBackMainWindow()));



    QVBoxLayout *vLayout = new QVBoxLayout(centralWidget);
    vLayout->addStretch(1);

    this->setStyleSheet("loginWindow{border-image: url(:/img/aaa.png);}");

    QHBoxLayout *hLayout = new QHBoxLayout();
    vLayout->addLayout(hLayout);

    hLayout->addStretch(1);

    QWidget *loginForm = new QWidget();
    QVBoxLayout *loginForm_VLayout = new QVBoxLayout(loginForm);
    loginForm_VLayout->addStretch(1);

    // 显示标签
    QLabel *lb = new QLabel(loginForm);

    lb->setFixedWidth(500);
    lb->setFixedHeight(80);
    lb->setText("图书管理系统");
    lb->setAlignment(Qt::AlignCenter);
    lb->setStyleSheet("font-size: 48px; color: rgb(255, 0, 0);");
    loginForm_VLayout->addWidget(lb);

    loginForm_VLayout->addSpacing(50);

    // 账号容器
    QWidget *usernameBox = new QWidget(loginForm);

    QHBoxLayout *usernameBox_HLayout = new QHBoxLayout(usernameBox);
    usernameBox_HLayout->addStretch(1);

    // 显示账号
    accountLineEdit = new QLineEdit(usernameBox);
    accountLineEdit->setFixedWidth(300);
    accountLineEdit->setFixedHeight(45);
    accountLineEdit->setMaxLength(12);
    accountLineEdit->setStyleSheet("font-size: 24px;");
    accountLineEdit->setPlaceholderText("请输入账号");
    usernameBox_HLayout->addWidget(accountLineEdit);

    // 隐藏按钮
    QPushButton *accountButton = new QPushButton("", usernameBox);
    accountButton->setFixedWidth(40);
    accountButton->setFixedHeight(40);
    //    accountButton->setVisible(false);
    usernameBox_HLayout->addWidget(accountButton);
    connect(accountButton,SIGNAL(clicked()),this,SLOT(fun1()));
    connect(accountButton,SIGNAL(clicked()),this,SLOT(fun2()));

    usernameBox_HLayout->addStretch(1);
    loginForm_VLayout->addWidget(usernameBox, 0, Qt::AlignHCenter);

    loginForm_VLayout->addSpacing(20);

    // 密码容器
    QWidget *passwordBox = new QWidget(loginForm);

    QHBoxLayout *passwordBox_HLayout = new QHBoxLayout(passwordBox);
    passwordBox_HLayout->addStretch(1);

    // 显示密码
    password = new QLineEdit(passwordBox);
    password->setFixedWidth(300);
    password->setFixedHeight(45);
    password->setMaxLength(12);
    password->setEchoMode(QLineEdit::Password);
    password->setStyleSheet("font-size: 24px;");
    passwordBox_HLayout->addWidget(password);

    // 显示明暗文密码
    QIcon icon(":/img/bbb.png");
    QPushButton *passwordVisibilityButton = new QPushButton(icon, "", passwordBox);
    passwordVisibilityButton->setIconSize(QSize(35, 35));
    passwordVisibilityButton->setFixedWidth(40);
    passwordVisibilityButton->setFixedHeight(40);
    passwordBox_HLayout->addWidget(passwordVisibilityButton);
    connect(passwordVisibilityButton, SIGNAL(pressed()), this, SLOT(passwordVisibility_pressed())); // 手动连接信号槽
    connect(passwordVisibilityButton, SIGNAL(released()), this, SLOT(passwordVisibility_released())); // 手动连接信号槽

    passwordBox_HLayout->addStretch(1);

    loginForm_VLayout->addWidget(passwordBox, 0, Qt::AlignHCenter);

    loginForm_VLayout->addSpacing(30);

    // 显示按钮
    QWidget *btnForm = new QWidget();

    QHBoxLayout *btnHLayout = new QHBoxLayout(btnForm);
    btnHLayout->addStretch(1);

    QPushButton *login_btn = new QPushButton("登录", btnForm);

    login_btn->setObjectName("login_btn");
    login_btn->setFixedWidth(100);
    login_btn->setFixedHeight(60);
    login_btn->setStyleSheet("#login_btn{background-color:rgb(255,255,255); font-size: 32px; border-radius: 15px;} QPushButton:pressed#login_btn{background-color:rgb(143,143,143);}");
    btnHLayout->addWidget(login_btn);
    connect(login_btn, SIGNAL(clicked()), this, SLOT(login())); // 手动连接信号槽

    btnHLayout->addStretch(1);

    QPushButton *register_btn = new QPushButton("注册", btnForm);
    register_btn->setObjectName("register_btn");
    register_btn->setFixedWidth(100);
    register_btn->setFixedHeight(60);
    register_btn->setStyleSheet("#register_btn{background-color:rgb(255,255,255); font-size: 32px; border-radius: 15px;} QPushButton:pressed#register_btn{background-color:rgb(143,143,143);}");
    btnHLayout->addWidget(register_btn);
    connect(register_btn, SIGNAL(clicked()), this, SLOT(register_btn())); // 手动连接信号槽


    btnHLayout->addStretch(1);

    loginForm_VLayout->addWidget(btnForm);

    loginForm_VLayout->addStretch(1);

    hLayout->addWidget(loginForm);

    hLayout->addStretch(1);

    vLayout->addStretch(1);
}

loginWindow::~loginWindow()
{
    delete ui;
}

QString loginWindow::getAccount()
{
    return account;
}

void loginWindow::login()
{
    if (count >= 3)
    {
        QMessageBox msgBox;
        msgBox.setWindowTitle("提示");
        QIcon con = QIcon(":/img/aaa.png");

        auto ret = QMessageBox::critical(this,"提示","账号密码框已经锁住",QMessageBox::Ok|QMessageBox::Cancel);
        if (ret == QMessageBox::Ok)
        {
            qDebug() << "ok";
        } else if (ret == QMessageBox::Cancel)
        {
            qDebug() << "Cancel";
        }
        return;
    }

    if (accountLineEdit->text() == "gec")
    {
        if (password->text() == "123456")
        {
            qDebug() << "登录成功";
            account = accountLineEdit->text();
            MainPageWindow * mainPageWindow = new MainPageWindow(this);
            // 绑定槽函数
            connect(this,SIGNAL(loginsignal(QString)),mainPageWindow,SLOT(loginSignedFunc(QString)));
            // 手动触发自定义信号
            emit loginsignal(account);
            mainPageWindow->show();
            this->hide();
            count = 0;
        }
        else
        {
           count++;
           auto ret = QMessageBox::warning(this,"提示","密码错误",QMessageBox::Ok|QMessageBox::Cancel);
           if (ret == QMessageBox::Ok)
           {
               qDebug() << "ok";
           } else if (ret == QMessageBox::Cancel)
           {
               qDebug() << "Cancel";
           }
        }
    }
    else
    {
        count++;
        auto ret = QMessageBox::information(this,"提示","账号错误",QMessageBox::Ok|QMessageBox::Cancel);
        if (ret == QMessageBox::Ok)
        {
            qDebug() << "ok";
        } else if (ret == QMessageBox::Cancel)
        {
            qDebug() << "Cancel";
        }
    }

    if (count >= 3)
    {
        accountLineEdit->setEnabled(false);
        password->setEnabled(false);
        auto ret = QMessageBox::question(this,"提示","错误太多，系统已经锁住",QMessageBox::Ok|QMessageBox::Cancel);
        if (ret == QMessageBox::Ok)
        {
            qDebug() << "ok";
        } else if (ret == QMessageBox::Cancel)
        {
            qDebug() << "Cancel";
        }
    }
}

void loginWindow::register_btn(){
    registerDialog * rd = new registerDialog(this);
//    rd->show(); //非模态对话框
    rd->exec(); // 模态对话框
}

void loginWindow::passwordVisibility_pressed()
{
    password->setEchoMode(QLineEdit::Normal);
}

void loginWindow::passwordVisibility_released()
{
    password->setEchoMode(QLineEdit::Password);
}

void loginWindow::fun1(){
    qDebug() << "fun1";
}

void loginWindow::fun2(){
    qDebug() << "fun2";
}

void loginWindow::reBackMainWindow(){
    QWidget * main_winodw = this->parentWidget();
    main_winodw->show();
//    parent->show();
    this->close();
}
